home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / hplip / base / status.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-10-28  |  30KB  |  1,201 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from __future__ import division
  5. import struct
  6. import cStringIO
  7. import xml.parsers.expat as expat
  8. import re
  9. from g import *
  10. from codes import *
  11. import pml
  12. import utils
  13. STATUS_REV_00 = 0
  14. STATUS_REV_01 = 1
  15. STATUS_REV_02 = 2
  16. STATUS_REV_03 = 3
  17. STATUS_REV_04 = 4
  18. STATUS_REV_V = 255
  19. STATUS_REV_UNKNOWN = 254
  20. vstatus_xlate = {
  21.     'busy': STATUS_PRINTER_BUSY,
  22.     'idle': STATUS_PRINTER_IDLE,
  23.     'prnt': STATUS_PRINTER_PRINTING,
  24.     'offf': STATUS_PRINTER_TURNING_OFF,
  25.     'rprt': STATUS_PRINTER_REPORT_PRINTING,
  26.     'cncl': STATUS_PRINTER_CANCELING,
  27.     'iost': STATUS_PRINTER_IO_STALL,
  28.     'dryw': STATUS_PRINTER_DRY_WAIT_TIME,
  29.     'penc': STATUS_PRINTER_PEN_CHANGE,
  30.     'oopa': STATUS_PRINTER_OUT_OF_PAPER,
  31.     'bnej': STATUS_PRINTER_BANNER_EJECT,
  32.     'bnmz': STATUS_PRINTER_BANNER_MISMATCH,
  33.     'phmz': STATUS_PRINTER_PHOTO_MISMATCH,
  34.     'dpmz': STATUS_PRINTER_DUPLEX_MISMATCH,
  35.     'pajm': STATUS_PRINTER_MEDIA_JAM,
  36.     'cars': STATUS_PRINTER_CARRIAGE_STALL,
  37.     'paps': STATUS_PRINTER_PAPER_STALL,
  38.     'penf': STATUS_PRINTER_PEN_FAILURE,
  39.     'erro': STATUS_PRINTER_HARD_ERROR,
  40.     'pwdn': STATUS_PRINTER_POWER_DOWN,
  41.     'fpts': STATUS_PRINTER_FRONT_PANEL_TEST,
  42.     'clno': STATUS_PRINTER_CLEAN_OUT_TRAY_MISSING }
  43. REVISION_2_TYPE_MAP = {
  44.     0: AGENT_TYPE_NONE,
  45.     1: AGENT_TYPE_BLACK,
  46.     2: AGENT_TYPE_CYAN,
  47.     3: AGENT_TYPE_MAGENTA,
  48.     4: AGENT_TYPE_YELLOW,
  49.     5: AGENT_TYPE_BLACK,
  50.     6: AGENT_TYPE_CYAN,
  51.     7: AGENT_TYPE_MAGENTA,
  52.     8: AGENT_TYPE_YELLOW }
  53. STATUS_BLOCK_UNKNOWN = {
  54.     'revision': STATUS_REV_UNKNOWN,
  55.     'agents': [],
  56.     'status-code': STATUS_UNKNOWN }
  57. NUM_PEN_POS = {
  58.     STATUS_REV_00: 16,
  59.     STATUS_REV_01: 16,
  60.     STATUS_REV_02: 16,
  61.     STATUS_REV_03: 18,
  62.     STATUS_REV_04: 22 }
  63. PEN_DATA_SIZE = {
  64.     STATUS_REV_00: 8,
  65.     STATUS_REV_01: 8,
  66.     STATUS_REV_02: 4,
  67.     STATUS_REV_03: 8,
  68.     STATUS_REV_04: 8 }
  69. STATUS_POS = {
  70.     STATUS_REV_00: 14,
  71.     STATUS_REV_01: 14,
  72.     STATUS_REV_02: 14,
  73.     STATUS_REV_03: 16,
  74.     STATUS_REV_04: 20 }
  75.  
  76. def parseSStatus(s, z = ''):
  77.     revision = ''
  78.     pens = []
  79.     top_door = TOP_DOOR_NOT_PRESENT
  80.     stat = STATUS_UNKNOWN
  81.     supply_door = SUPPLY_DOOR_NOT_PRESENT
  82.     duplexer = DUPLEXER_NOT_PRESENT
  83.     photo_tray = PHOTO_TRAY_NOT_PRESENT
  84.     in_tray1 = IN_TRAY_NOT_PRESENT
  85.     in_tray2 = IN_TRAY_NOT_PRESENT
  86.     media_path = MEDIA_PATH_NOT_PRESENT
  87.     Z_SIZE = 6
  88.     
  89.     try:
  90.         z1 = []
  91.         if len(z) > 0:
  92.             z_fields = z.split(',')
  93.             for z_field in z_fields:
  94.                 if len(z_field) > 2 and z_field[:2] == '05':
  95.                     z1s = z_field[2:]
  96.                     z1 = [ int(x, 16) for x in z1s ]
  97.                     continue
  98.                 []
  99.             
  100.         
  101.         s1 = [ int(x, 16) for x in s ]
  102.         revision = s1[1]
  103.         [] if revision <= revision else []
  104.         top_door = bool(s1[2] & 0x8L) + s1[2] & 0x1L
  105.         supply_door = bool(s1[3] & 0x8L) + s1[3] & 0x1L
  106.         duplexer = bool(s1[4] & 0xCL) + s1[4] & 0x1L
  107.         photo_tray = bool(s1[5] & 0x8L) + s1[5] & 0x1L
  108.         if revision == STATUS_REV_02:
  109.             in_tray1 = bool(s1[6] & 0x8L) + s1[6] & 0x1L
  110.             in_tray2 = bool(s1[7] & 0x8L) + s1[7] & 0x1L
  111.         else:
  112.             in_tray1 = bool(s1[6] & 0x8L)
  113.             in_tray2 = bool(s1[7] & 0x8L)
  114.         media_path = bool(s1[8] & 0x8L) + (s1[8] & 0x1L) + (bool(s1[18] & 0x2L) << 1)
  115.         status_pos = STATUS_POS[revision]
  116.         status_byte = (s1[status_pos] << 4) + s1[status_pos + 1]
  117.         stat = status_byte + STATUS_PRINTER_BASE
  118.         pen = { }
  119.         c = NUM_PEN_POS[revision] + 1
  120.         d = 0
  121.         num_pens = s1[NUM_PEN_POS[revision]]
  122.         log.debug('Num pens=%d' % num_pens)
  123.         index = 0
  124.         pen_data_size = PEN_DATA_SIZE[revision]
  125.         for p in range(num_pens):
  126.             info = long(s[c:c + pen_data_size], 16)
  127.             pen['index'] = index
  128.             if pen_data_size == 4:
  129.                 pen['type'] = REVISION_2_TYPE_MAP.get(int((info & 0xF000L) >> 0xCL), 0)
  130.                 if index < num_pens / 2:
  131.                     pen['kind'] = AGENT_KIND_HEAD
  132.                 else:
  133.                     pen['kind'] = AGENT_KIND_SUPPLY
  134.                 pen['level-trigger'] = int((info & 0xE00L) >> 0x9L)
  135.                 pen['health'] = int((info & 0x180L) >> 0x7L)
  136.                 pen['level'] = int(info & 0x7FL)
  137.                 pen['id'] = 31
  138.             elif pen_data_size == 8:
  139.                 pen['kind'] = bool(info & 0x80000000L) + (bool(info & 0x40000000L) << 0x1L)
  140.                 pen['type'] = int((info & 0x3F000000L) >> 0x18L)
  141.                 pen['id'] = int((info & 16252928) >> 0x13L)
  142.                 pen['level-trigger'] = int((info & 0x70000L) >> 0x10L)
  143.                 pen['health'] = int((info & 0xC000L) >> 0xEL)
  144.                 pen['level'] = int(info & 0xFFL)
  145.             else:
  146.                 log.error('Pen data size error')
  147.             if len(z1) > 0:
  148.                 
  149.                 try:
  150.                     pen['dvc'] = long(z1s[d + 1:d + 5], 16)
  151.                     pen['virgin'] = bool(z1[d + 5] & 0x8L)
  152.                     pen['hp-ink'] = bool(z1[d + 5] & 0x4L)
  153.                     pen['known'] = bool(z1[d + 5] & 0x2L)
  154.                     pen['ack'] = bool(z1[d + 5] & 0x1L)
  155.                 except IndexError:
  156.                     pen['dvc'] = 0
  157.                     pen['virgin'] = 0
  158.                     pen['hp-ink'] = 0
  159.                     pen['known'] = 0
  160.                     pen['ack'] = 0
  161.                 except:
  162.                     None<EXCEPTION MATCH>IndexError
  163.                 
  164.  
  165.             None<EXCEPTION MATCH>IndexError
  166.             index += 1
  167.             pens.append(pen)
  168.             log.debug('Pen %d: %s' % (p, pen))
  169.             pen = { }
  170.             c += pen_data_size
  171.             d += Z_SIZE
  172.     except (IndexError, ValueError, TypeError):
  173.         e = None
  174.         log.warn('Status parsing error: %s' % str(e))
  175.  
  176.     return {
  177.         'revision': revision,
  178.         'agents': pens,
  179.         'top-door': top_door,
  180.         'status-code': stat,
  181.         'supply-door': supply_door,
  182.         'duplexer': duplexer,
  183.         'photo-tray': photo_tray,
  184.         'in-tray1': in_tray1,
  185.         'in-tray2': in_tray2,
  186.         'media-path': media_path }
  187.  
  188.  
  189. def parseVStatus(s):
  190.     pens = []
  191.     pen = { }
  192.     c = 0
  193.     fields = s.split(',')
  194.     log.debug(fields)
  195.     f0 = fields[0]
  196.     if len(f0) == 20:
  197.         pass
  198.     elif len(f0) == 8:
  199.         for p in f0:
  200.             if c == 0:
  201.                 c += 1
  202.                 continue
  203.             if c == 1:
  204.                 if p in ('a', 'A'):
  205.                     pen['type'] = AGENT_TYPE_NONE
  206.                     pen['kind'] = AGENT_KIND_NONE
  207.                 
  208.                 c += 1
  209.                 continue
  210.             if c == 2:
  211.                 pen['health'] = AGENT_HEALTH_OK
  212.                 pen['kind'] = AGENT_KIND_HEAD_AND_SUPPLY
  213.                 if p in ('b', 'B'):
  214.                     pen['type'] = AGENT_TYPE_BLACK
  215.                 elif p in ('c', 'C'):
  216.                     pen['type'] = AGENT_TYPE_CMY
  217.                 elif p in ('d', 'D'):
  218.                     pen['type'] = AGENT_TYPE_KCM
  219.                 elif p in ('u', 'U'):
  220.                     pen['type'] = AGENT_TYPE_NONE
  221.                     pen['health'] = AGENT_HEALTH_MISINSTALLED
  222.                 
  223.                 c += 1
  224.                 continue
  225.             if c == 3:
  226.                 if p == '0':
  227.                     pen['state'] = 1
  228.                 else:
  229.                     pen['state'] = 0
  230.                 pen['level'] = 0
  231.                 i = 8
  232.                 while True:
  233.                     
  234.                     try:
  235.                         f = fields[i]
  236.                     except IndexError:
  237.                         break
  238.  
  239.                     if f[:2] == 'KP' and pen['type'] == AGENT_TYPE_BLACK:
  240.                         pen['level'] = int(f[2:])
  241.                     elif f[:2] == 'CP' and pen['type'] == AGENT_TYPE_CMY:
  242.                         pen['level'] = int(f[2:])
  243.                     
  244.                     i += 1
  245.                 pens.append(pen)
  246.                 pen = { }
  247.                 c = 0
  248.                 continue
  249.         
  250.     
  251.     
  252.     try:
  253.         fields[2]
  254.     except IndexError:
  255.         top_lid = 1
  256.  
  257.     if fields[2] == 'DN':
  258.         top_lid = 1
  259.     else:
  260.         top_lid = 2
  261.     
  262.     try:
  263.         stat = vstatus_xlate.get(fields[3].lower(), STATUS_PRINTER_IDLE)
  264.     except IndexError:
  265.         stat = STATUS_PRINTER_IDLE
  266.  
  267.     return {
  268.         'revision': STATUS_REV_V,
  269.         'agents': pens,
  270.         'top-door': top_lid,
  271.         'status-code': stat,
  272.         'supply-door': SUPPLY_DOOR_NOT_PRESENT,
  273.         'duplexer': DUPLEXER_NOT_PRESENT,
  274.         'photo-tray': PHOTO_TRAY_NOT_PRESENT,
  275.         'in-tray1': IN_TRAY_NOT_PRESENT,
  276.         'in-tray2': IN_TRAY_NOT_PRESENT,
  277.         'media-path': MEDIA_PATH_CUT_SHEET }
  278.  
  279.  
  280. def parseStatus(DeviceID):
  281.     if 'VSTATUS' in DeviceID:
  282.         return parseVStatus(DeviceID['VSTATUS'])
  283.     if 'S' in DeviceID:
  284.         return parseSStatus(DeviceID['S'], DeviceID.get('Z', ''))
  285.     return STATUS_BLOCK_UNKNOWN
  286.  
  287.  
  288. def LaserJetDeviceStatusToPrinterStatus(device_status, printer_status, detected_error_state):
  289.     stat = STATUS_PRINTER_IDLE
  290.     if device_status in (pml.DEVICE_STATUS_WARNING, pml.DEVICE_STATUS_DOWN):
  291.         if detected_error_state & pml.DETECTED_ERROR_STATE_LOW_PAPER_MASK and not (detected_error_state & pml.DETECTED_ERROR_STATE_NO_PAPER_MASK):
  292.             stat = STATUS_PRINTER_LOW_PAPER
  293.         elif detected_error_state & pml.DETECTED_ERROR_STATE_NO_PAPER_MASK:
  294.             stat = STATUS_PRINTER_OUT_OF_PAPER
  295.         elif detected_error_state & pml.DETECTED_ERROR_STATE_DOOR_OPEN_MASK:
  296.             stat = STATUS_PRINTER_DOOR_OPEN
  297.         elif detected_error_state & pml.DETECTED_ERROR_STATE_JAMMED_MASK:
  298.             stat = STATUS_PRINTER_MEDIA_JAM
  299.         elif detected_error_state & pml.DETECTED_ERROR_STATE_OUT_CART_MASK:
  300.             stat = STATUS_PRINTER_NO_TONER
  301.         elif detected_error_state & pml.DETECTED_ERROR_STATE_LOW_CART_MASK:
  302.             stat = STATUS_PRINTER_LOW_TONER
  303.         elif detected_error_state == pml.DETECTED_ERROR_STATE_SERVICE_REQUEST_MASK:
  304.             stat = STATUS_PRINTER_SERVICE_REQUEST
  305.         elif detected_error_state & pml.DETECTED_ERROR_STATE_OFFLINE_MASK:
  306.             stat = STATUS_PRINTER_OFFLINE
  307.         
  308.     elif printer_status == pml.PRINTER_STATUS_IDLE:
  309.         stat = STATUS_PRINTER_IDLE
  310.     elif printer_status == pml.PRINTER_STATUS_PRINTING:
  311.         stat = STATUS_PRINTER_PRINTING
  312.     elif printer_status == pml.PRINTER_STATUS_WARMUP:
  313.         stat = STATUS_PRINTER_WARMING_UP
  314.     
  315.     return stat
  316.  
  317. COLORANT_INDEX_TO_AGENT_TYPE_MAP = {
  318.     'other': AGENT_TYPE_UNSPECIFIED,
  319.     'unknown': AGENT_TYPE_UNSPECIFIED,
  320.     'blue': AGENT_TYPE_BLUE,
  321.     'cyan': AGENT_TYPE_CYAN,
  322.     'magenta': AGENT_TYPE_MAGENTA,
  323.     'yellow': AGENT_TYPE_YELLOW,
  324.     'black': AGENT_TYPE_BLACK }
  325. MARKER_SUPPLES_TYPE_TO_AGENT_KIND_MAP = {
  326.     pml.OID_MARKER_SUPPLIES_TYPE_OTHER: AGENT_KIND_UNKNOWN,
  327.     pml.OID_MARKER_SUPPLIES_TYPE_UNKNOWN: AGENT_KIND_UNKNOWN,
  328.     pml.OID_MARKER_SUPPLIES_TYPE_TONER: AGENT_KIND_TONER_CARTRIDGE,
  329.     pml.OID_MARKER_SUPPLIES_TYPE_WASTE_TONER: AGENT_KIND_UNKNOWN,
  330.     pml.OID_MARKER_SUPPLIES_TYPE_INK: AGENT_KIND_SUPPLY,
  331.     pml.OID_MARKER_SUPPLIES_TYPE_INK_CART: AGENT_KIND_HEAD_AND_SUPPLY,
  332.     pml.OID_MARKER_SUPPLIES_TYPE_INK_RIBBON: AGENT_KIND_HEAD_AND_SUPPLY,
  333.     pml.OID_MARKER_SUPPLIES_TYPE_WASTE_INK: AGENT_KIND_UNKNOWN,
  334.     pml.OID_MARKER_SUPPLIES_TYPE_OPC: AGENT_KIND_DRUM_KIT,
  335.     pml.OID_MARKER_SUPPLIES_TYPE_DEVELOPER: AGENT_KIND_UNKNOWN,
  336.     pml.OID_MARKER_SUPPLIES_TYPE_FUSER_OIL: AGENT_KIND_UNKNOWN,
  337.     pml.OID_MARKER_SUPPLIES_TYPE_SOLID_WAX: AGENT_KIND_UNKNOWN,
  338.     pml.OID_MARKER_SUPPLIES_TYPE_RIBBON_WAX: AGENT_KIND_UNKNOWN,
  339.     pml.OID_MARKER_SUPPLIES_TYPE_WASTE_WAX: AGENT_KIND_UNKNOWN,
  340.     pml.OID_MARKER_SUPPLIES_TYPE_FUSER: AGENT_KIND_MAINT_KIT,
  341.     pml.OID_MARKER_SUPPLIES_TYPE_CORONA_WIRE: AGENT_KIND_UNKNOWN,
  342.     pml.OID_MARKER_SUPPLIES_TYPE_FUSER_OIL_WICK: AGENT_KIND_UNKNOWN,
  343.     pml.OID_MARKER_SUPPLIES_TYPE_CLEANER_UNIT: AGENT_KIND_UNKNOWN,
  344.     pml.OID_MARKER_SUPPLIES_TYPE_FUSER_CLEANING_PAD: AGENT_KIND_UNKNOWN,
  345.     pml.OID_MARKER_SUPPLIES_TYPE_TRANSFER_UNIT: AGENT_KIND_TRANSFER_KIT,
  346.     pml.OID_MARKER_SUPPLIES_TYPE_TONER_CART: AGENT_KIND_TONER_CARTRIDGE,
  347.     pml.OID_MARKER_SUPPLIES_TYPE_FUSER_OILER: AGENT_KIND_UNKNOWN,
  348.     pml.OID_MARKER_SUPPLIES_TYPE_ADF_MAINT_KIT: AGENT_KIND_ADF_KIT }
  349.  
  350. def StatusType3(dev, parsedID):
  351.     
  352.     try:
  353.         dev.openPML()
  354.         (result_code, printer_status) = dev.getPML(pml.OID_PRINTER_STATUS, pml.INT_SIZE_BYTE)
  355.         (result_code, device_status) = dev.getPML(pml.OID_DEVICE_STATUS, pml.INT_SIZE_BYTE)
  356.         (result_code, cover_status) = dev.getPML(pml.OID_COVER_STATUS, pml.INT_SIZE_BYTE)
  357.         (result_code, value) = dev.getPML(pml.OID_DETECTED_ERROR_STATE)
  358.     except Error:
  359.         dev.closePML()
  360.         return {
  361.             'revision': STATUS_REV_UNKNOWN,
  362.             'agents': [],
  363.             'top-door': 0,
  364.             'status-code': STATUS_UNKNOWN,
  365.             'supply-door': 0,
  366.             'duplexer': 1,
  367.             'photo-tray': 0,
  368.             'in-tray1': 0,
  369.             'in-tray2': 0,
  370.             'media-path': 0 }
  371.  
  372.     
  373.     try:
  374.         detected_error_state = struct.unpack('B', value[0])[0]
  375.     except (IndexError, TypeError):
  376.         detected_error_state = pml.DETECTED_ERROR_STATE_OFFLINE_MASK
  377.  
  378.     agents = []
  379.     x = 1
  380.     while True:
  381.         log.debug('%s Agent: %d %s' % ('**********', x, '**********'))
  382.         log.debug('OID_MARKER_SUPPLIES_TYPE_%d:' % x)
  383.         oid = (pml.OID_MARKER_SUPPLIES_TYPE_x % x, pml.OID_MARKER_SUPPLIES_TYPE_x_TYPE)
  384.         (result_code, value) = dev.getPML(oid, pml.INT_SIZE_BYTE)
  385.         if result_code != ERROR_SUCCESS or value is None:
  386.             log.debug('End of supply information.')
  387.             break
  388.         
  389.         for a in MARKER_SUPPLES_TYPE_TO_AGENT_KIND_MAP:
  390.             if value == a:
  391.                 agent_kind = MARKER_SUPPLES_TYPE_TO_AGENT_KIND_MAP[a]
  392.                 break
  393.                 continue
  394.         else:
  395.             agent_kind = AGENT_KIND_UNKNOWN
  396.         log.debug('OID_MARKER_SUPPLIES_LEVEL_%d:' % x)
  397.         oid = (pml.OID_MARKER_SUPPLIES_LEVEL_x % x, pml.OID_MARKER_SUPPLIES_LEVEL_x_TYPE)
  398.         (result_code, agent_level) = dev.getPML(oid)
  399.         if result_code != ERROR_SUCCESS:
  400.             log.debug('Failed')
  401.             break
  402.         
  403.         log.debug('agent%d-level: %d' % (x, agent_level))
  404.         log.debug('OID_MARKER_SUPPLIES_MAX_%d:' % x)
  405.         oid = (pml.OID_MARKER_SUPPLIES_MAX_x % x, pml.OID_MARKER_SUPPLIES_MAX_x_TYPE)
  406.         (result_code, agent_max) = dev.getPML(oid)
  407.         if agent_max == 0:
  408.             agent_max = 1
  409.         
  410.         if result_code != ERROR_SUCCESS:
  411.             log.debug('Failed')
  412.             break
  413.         
  414.         log.debug('agent%d-max: %d' % (x, agent_max))
  415.         log.debug('OID_MARKER_SUPPLIES_COLORANT_INDEX_%d:' % x)
  416.         oid = (pml.OID_MARKER_SUPPLIES_COLORANT_INDEX_x % x, pml.OID_MARKER_SUPPLIES_COLORANT_INDEX_x_TYPE)
  417.         (result_code, colorant_index) = dev.getPML(oid)
  418.         if result_code != ERROR_SUCCESS:
  419.             log.debug('Failed')
  420.             agent_type = AGENT_TYPE_BLACK
  421.         else:
  422.             log.debug('Colorant index: %d' % colorant_index)
  423.             log.debug('OID_MARKER_COLORANT_VALUE_%d' % x)
  424.             oid = (pml.OID_MARKER_COLORANT_VALUE_x % colorant_index, pml.OID_MARKER_COLORANT_VALUE_x_TYPE)
  425.             (result_code, colorant_value) = dev.getPML(oid)
  426.             if result_code != ERROR_SUCCESS:
  427.                 log.debug('Failed. Defaulting to black.')
  428.                 agent_type = AGENT_TYPE_BLACK
  429.             
  430.             if agent_kind in (AGENT_KIND_MAINT_KIT, AGENT_KIND_ADF_KIT, AGENT_KIND_DRUM_KIT, AGENT_KIND_TRANSFER_KIT):
  431.                 agent_type = AGENT_TYPE_UNSPECIFIED
  432.             else:
  433.                 agent_type = AGENT_TYPE_BLACK
  434.                 if result_code != ERROR_SUCCESS:
  435.                     log.debug('OID_MARKER_SUPPLIES_DESCRIPTION_%d:' % x)
  436.                     oid = (pml.OID_MARKER_SUPPLIES_DESCRIPTION_x % x, pml.OID_MARKER_SUPPLIES_DESCRIPTION_x_TYPE)
  437.                     (result_code, colorant_value) = dev.getPML(oid)
  438.                     if result_code != ERROR_SUCCESS:
  439.                         log.debug('Failed')
  440.                         break
  441.                     
  442.                     if colorant_value is not None:
  443.                         log.debug('colorant value: %s' % colorant_value)
  444.                         colorant_value = colorant_value.lower().strip()
  445.                         for c in COLORANT_INDEX_TO_AGENT_TYPE_MAP:
  446.                             if colorant_value.find(c) >= 0:
  447.                                 agent_type = COLORANT_INDEX_TO_AGENT_TYPE_MAP[c]
  448.                                 break
  449.                                 continue
  450.                         else:
  451.                             agent_type = AGENT_TYPE_BLACK
  452.                     
  453.                 elif colorant_value is not None:
  454.                     log.debug('colorant value: %s' % colorant_value)
  455.                     agent_type = COLORANT_INDEX_TO_AGENT_TYPE_MAP.get(colorant_value, AGENT_TYPE_BLACK)
  456.                 
  457.                 if agent_type == AGENT_TYPE_NONE:
  458.                     if agent_kind == AGENT_KIND_TONER_CARTRIDGE:
  459.                         agent_type = AGENT_TYPE_BLACK
  460.                     else:
  461.                         agent_type = AGENT_TYPE_UNSPECIFIED
  462.                 
  463.         log.debug('OID_MARKER_STATUS_%d:' % x)
  464.         oid = (pml.OID_MARKER_STATUS_x % x, pml.OID_MARKER_STATUS_x_TYPE)
  465.         (result_code, agent_status) = dev.getPML(oid)
  466.         if result_code != ERROR_SUCCESS:
  467.             log.debug('Failed')
  468.             agent_trigger = AGENT_LEVEL_TRIGGER_SUFFICIENT_0
  469.             agent_health = AGENT_HEALTH_OK
  470.         else:
  471.             agent_trigger = AGENT_LEVEL_TRIGGER_SUFFICIENT_0
  472.             if agent_status is None:
  473.                 agent_health = AGENT_HEALTH_OK
  474.             elif agent_status == pml.OID_MARKER_STATUS_OK:
  475.                 agent_health = AGENT_HEALTH_OK
  476.             elif agent_status == pml.OID_MARKER_STATUS_MISINSTALLED:
  477.                 agent_health = AGENT_HEALTH_MISINSTALLED
  478.             elif agent_status in (pml.OID_MARKER_STATUS_LOW_TONER_CONT, pml.OID_MARKER_STATUS_LOW_TONER_STOP):
  479.                 agent_health = AGENT_HEALTH_OK
  480.                 agent_trigger = AGENT_LEVEL_TRIGGER_MAY_BE_LOW
  481.             else:
  482.                 agent_health = AGENT_HEALTH_OK
  483.         agent_level = int((agent_level / agent_max) * 100)
  484.         log.debug('agent%d: kind=%d, type=%d, health=%d, level=%d, level-trigger=%d' % (x, agent_kind, agent_type, agent_health, agent_level, agent_trigger))
  485.         agents.append({
  486.             'kind': agent_kind,
  487.             'type': agent_type,
  488.             'health': agent_health,
  489.             'level': agent_level,
  490.             'level-trigger': agent_trigger })
  491.         x += 1
  492.         if x > 20:
  493.             break
  494.             continue
  495.     if not printer_status:
  496.         pass
  497.     printer_status = STATUS_PRINTER_IDLE
  498.     log.debug('printer_status=%d' % printer_status)
  499.     if not device_status:
  500.         pass
  501.     device_status = pml.DEVICE_STATUS_RUNNING
  502.     log.debug('device_status=%d' % device_status)
  503.     if not cover_status:
  504.         pass
  505.     cover_status = pml.COVER_STATUS_CLOSED
  506.     log.debug('cover_status=%d' % cover_status)
  507.     if not detected_error_state:
  508.         pass
  509.     detected_error_state = pml.DETECTED_ERROR_STATE_NO_ERROR
  510.     log.debug('detected_error_state=%d (0x%x)' % (detected_error_state, detected_error_state))
  511.     stat = LaserJetDeviceStatusToPrinterStatus(device_status, printer_status, detected_error_state)
  512.     log.debug('Printer status=%d' % stat)
  513.     if stat == STATUS_PRINTER_DOOR_OPEN:
  514.         supply_door = 0
  515.     else:
  516.         supply_door = 1
  517.     return {
  518.         'revision': STATUS_REV_UNKNOWN,
  519.         'agents': agents,
  520.         'top-door': cover_status,
  521.         'status-code': stat,
  522.         'supply-door': supply_door,
  523.         'duplexer': 1,
  524.         'photo-tray': 0,
  525.         'in-tray1': 1,
  526.         'in-tray2': 1,
  527.         'media-path': 1 }
  528.  
  529.  
  530. def setup_panel_translator():
  531.     global PANEL_TRANSLATOR_FUNC
  532.     printables = list('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\n!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~')
  533.     map = { }
  534.     for x in range(0, 256):
  535.         pass
  536.     
  537.     map.update({
  538.         '\x10': '\xab',
  539.         '\x11': '\xbb',
  540.         '\x12': '\xa3',
  541.         '\x13': '\xbb',
  542.         '\x80': '\xab',
  543.         '\x81': '\xbb',
  544.         '\x82': '*',
  545.         '\x83': '*',
  546.         '\x85': '*',
  547.         '\xa0': '\xab',
  548.         '\x1f': '?',
  549.         '=': ' ' })
  550.     (frm, to) = ('', '')
  551.     map_keys = map.keys()
  552.     map_keys.sort()
  553.     for x in map_keys:
  554.         frm = ''.join([
  555.             frm,
  556.             x])
  557.         to = ''.join([
  558.             to,
  559.             map[x]])
  560.     
  561.     PANEL_TRANSLATOR_FUNC = utils.Translator(frm, to)
  562.  
  563. PANEL_TRANSLATOR_FUNC = None
  564. setup_panel_translator()
  565.  
  566. def PanelCheck(dev):
  567.     (line1, line2) = ('', '')
  568.     if dev.io_mode not in (IO_MODE_RAW, IO_MODE_UNI):
  569.         
  570.         try:
  571.             dev.openPML()
  572.         except Error:
  573.             pass
  574.  
  575.         oids = [
  576.             (pml.OID_HP_LINE1, pml.OID_HP_LINE2),
  577.             (pml.OID_SPM_LINE1, pml.OID_SPM_LINE2)]
  578.         for oid1, oid2 in oids:
  579.             (result, line1) = dev.getPML(oid1)
  580.             if result < pml.ERROR_MAX_OK:
  581.                 line1 = PANEL_TRANSLATOR_FUNC(line1).rstrip()
  582.                 if '\n' in line1:
  583.                     (line1, line2) = line1.split('\n', 1)
  584.                     break
  585.                 
  586.                 (result, line2) = dev.getPML(oid2)
  587.                 if result < pml.ERROR_MAX_OK:
  588.                     line2 = PANEL_TRANSLATOR_FUNC(line2).rstrip()
  589.                     break
  590.                 
  591.             result < pml.ERROR_MAX_OK
  592.         
  593.     
  594.     if not line2:
  595.         pass
  596.     return (None, bool(line2) if not line1 else '', '')
  597.  
  598. BATTERY_HEALTH_MAP = {
  599.     0: AGENT_HEALTH_OK,
  600.     1: AGENT_HEALTH_OVERTEMP,
  601.     2: AGENT_HEALTH_CHARGING,
  602.     3: AGENT_HEALTH_MISINSTALLED,
  603.     4: AGENT_HEALTH_FAILED }
  604. BATTERY_TRIGGER_MAP = {
  605.     0: AGENT_LEVEL_TRIGGER_SUFFICIENT_0,
  606.     1: AGENT_LEVEL_TRIGGER_ALMOST_DEFINITELY_OUT,
  607.     2: AGENT_LEVEL_TRIGGER_PROBABLY_OUT,
  608.     3: AGENT_LEVEL_TRIGGER_SUFFICIENT_4,
  609.     4: AGENT_LEVEL_TRIGGER_SUFFICIENT_2,
  610.     5: AGENT_LEVEL_TRIGGER_SUFFICIENT_0 }
  611. BATTERY_PML_TRIGGER_MAP = {
  612.     (100, 80): AGENT_LEVEL_TRIGGER_SUFFICIENT_0,
  613.     (79, 60): AGENT_LEVEL_TRIGGER_SUFFICIENT_1,
  614.     (59, 40): AGENT_LEVEL_TRIGGER_SUFFICIENT_2,
  615.     (39, 30): AGENT_LEVEL_TRIGGER_SUFFICIENT_3,
  616.     (29, 20): AGENT_LEVEL_TRIGGER_SUFFICIENT_4,
  617.     (19, 10): AGENT_LEVEL_TRIGGER_MAY_BE_LOW,
  618.     (9, 5): AGENT_LEVEL_TRIGGER_PROBABLY_OUT,
  619.     (4, -1): AGENT_LEVEL_TRIGGER_ALMOST_DEFINITELY_OUT }
  620.  
  621. def BatteryCheck(dev, status_block, battery_check):
  622.     try_dynamic_counters = False
  623.     
  624.     try:
  625.         dev.openPML()
  626.     except Error:
  627.         if battery_check == STATUS_BATTERY_CHECK_STD:
  628.             log.debug('PML channel open failed. Trying dynamic counters...')
  629.             try_dynamic_counters = True
  630.         
  631.     except:
  632.         battery_check == STATUS_BATTERY_CHECK_STD
  633.     elif battery_check == STATUS_BATTERY_CHECK_PML:
  634.         (result, battery_level) = dev.getPML(pml.OID_BATTERY_LEVEL_2)
  635.         if result > pml.ERROR_MAX_OK:
  636.             status_block['agents'].append({
  637.                 'kind': AGENT_KIND_INT_BATTERY,
  638.                 'type': AGENT_TYPE_UNSPECIFIED,
  639.                 'health': AGENT_HEALTH_UNKNOWN,
  640.                 'level': 0,
  641.                 'level-trigger': AGENT_LEVEL_TRIGGER_SUFFICIENT_0 })
  642.             return None
  643.         status_block['agents'].append({
  644.             'kind': AGENT_KIND_INT_BATTERY,
  645.             'type': AGENT_TYPE_UNSPECIFIED,
  646.             'health': AGENT_HEALTH_OK,
  647.             'level': battery_level,
  648.             'level-trigger': AGENT_LEVEL_TRIGGER_SUFFICIENT_0 })
  649.         return None
  650.     else:
  651.         (result, battery_level) = dev.getPML(pml.OID_BATTERY_LEVEL)
  652.         (result, power_mode) = dev.getPML(pml.OID_POWER_MODE)
  653.         if battery_level is not None and power_mode is not None:
  654.             if power_mode & pml.POWER_MODE_BATTERY_LEVEL_KNOWN and battery_level >= 0:
  655.                 for x in BATTERY_PML_TRIGGER_MAP:
  656.                     if battery_level >= battery_level:
  657.                         pass
  658.                     elif battery_level > x[1]:
  659.                         battery_trigger_level = BATTERY_PML_TRIGGER_MAP[x]
  660.                         break
  661.                         continue
  662.                 
  663.                 if power_mode & pml.POWER_MODE_CHARGING:
  664.                     agent_health = AGENT_HEALTH_CHARGING
  665.                 elif power_mode & pml.POWER_MODE_DISCHARGING:
  666.                     agent_health = AGENT_HEALTH_DISCHARGING
  667.                 else:
  668.                     agent_health = AGENT_HEALTH_OK
  669.                 status_block['agents'].append({
  670.                     'kind': AGENT_KIND_INT_BATTERY,
  671.                     'type': AGENT_TYPE_UNSPECIFIED,
  672.                     'health': agent_health,
  673.                     'level': battery_level,
  674.                     'level-trigger': battery_trigger_level })
  675.                 return None
  676.             status_block['agents'].append({
  677.                 'kind': AGENT_KIND_INT_BATTERY,
  678.                 'type': AGENT_TYPE_UNSPECIFIED,
  679.                 'health': AGENT_HEALTH_UNKNOWN,
  680.                 'level': 0,
  681.                 'level-trigger': AGENT_LEVEL_TRIGGER_SUFFICIENT_0 })
  682.             return None
  683.         power_mode is not None
  684.         try_dynamic_counters = True
  685.     finally:
  686.         dev.closePML()
  687.  
  688.     if battery_check == STATUS_BATTERY_CHECK_STD and try_dynamic_counters:
  689.         
  690.         try:
  691.             battery_health = dev.getDynamicCounter(200)
  692.             battery_trigger_level = dev.getDynamicCounter(201)
  693.             battery_level = dev.getDynamicCounter(202)
  694.             status_block['agents'].append({
  695.                 'kind': AGENT_KIND_INT_BATTERY,
  696.                 'type': AGENT_TYPE_UNSPECIFIED,
  697.                 'health': BATTERY_HEALTH_MAP[battery_health],
  698.                 'level': battery_level,
  699.                 'level-trigger': BATTERY_TRIGGER_MAP[battery_trigger_level] })
  700.         except Error:
  701.             result > pml.ERROR_MAX_OK
  702.             result > pml.ERROR_MAX_OK
  703.             status_block['agents'].append({
  704.                 'kind': AGENT_KIND_INT_BATTERY,
  705.                 'type': AGENT_TYPE_UNSPECIFIED,
  706.                 'health': AGENT_HEALTH_UNKNOWN,
  707.                 'level': 0,
  708.                 'level-trigger': AGENT_LEVEL_TRIGGER_SUFFICIENT_0 })
  709.         except:
  710.             result > pml.ERROR_MAX_OK
  711.         finally:
  712.             dev.closePrint()
  713.  
  714.     else:
  715.         status_block['agents'].append({
  716.             'kind': AGENT_KIND_INT_BATTERY,
  717.             'type': AGENT_TYPE_UNSPECIFIED,
  718.             'health': AGENT_HEALTH_UNKNOWN,
  719.             'level': 0,
  720.             'level-trigger': AGENT_LEVEL_TRIGGER_SUFFICIENT_0 })
  721.  
  722.  
  723. def getPenConfiguration(s):
  724.     pens = [ p['type'] for p in s['agents'] ]
  725.     if utils.all(pens, (lambda x: x == AGENT_TYPE_NONE)):
  726.         return AGENT_CONFIG_NONE
  727.     if AGENT_TYPE_NONE in pens:
  728.         if AGENT_TYPE_BLACK in pens:
  729.             return AGENT_CONFIG_BLACK_ONLY
  730.         if AGENT_TYPE_CMY in pens:
  731.             return AGENT_CONFIG_COLOR_ONLY
  732.         if AGENT_TYPE_KCM in pens:
  733.             return AGENT_CONFIG_PHOTO_ONLY
  734.         if AGENT_TYPE_GGK in pens:
  735.             return AGENT_CONFIG_GREY_ONLY
  736.         return AGENT_CONFIG_INVALID
  737.     AGENT_TYPE_NONE in pens
  738.     if AGENT_TYPE_BLACK in pens and AGENT_TYPE_CMY in pens:
  739.         return AGENT_CONFIG_COLOR_AND_BLACK
  740.     if AGENT_TYPE_CMY in pens and AGENT_TYPE_KCM in pens:
  741.         return AGENT_CONFIG_COLOR_AND_PHOTO
  742.     if AGENT_TYPE_CMY in pens and AGENT_TYPE_GGK in pens:
  743.         return AGENT_CONFIG_COLOR_AND_GREY
  744.     return AGENT_CONFIG_INVALID
  745.  
  746.  
  747. def getFaxStatus(dev):
  748.     tx_active = False
  749.     rx_active = False
  750.     if dev.io_mode not in (IO_MODE_UNI, IO_MODE_RAW):
  751.         
  752.         try:
  753.             dev.openPML()
  754.             (result_code, tx_state) = dev.getPML(pml.OID_FAXJOB_TX_STATUS)
  755.             if result_code == ERROR_SUCCESS and tx_state:
  756.                 if tx_state not in (pml.FAXJOB_TX_STATUS_IDLE, pml.FAXJOB_TX_STATUS_DONE):
  757.                     tx_active = True
  758.                 
  759.             
  760.             (result_code, rx_state) = dev.getPML(pml.OID_FAXJOB_RX_STATUS)
  761.             if result_code == ERROR_SUCCESS and rx_state:
  762.                 if rx_state not in (pml.FAXJOB_RX_STATUS_IDLE, pml.FAXJOB_RX_STATUS_DONE):
  763.                     rx_active = True
  764.                 
  765.         finally:
  766.             dev.closePML()
  767.  
  768.     
  769.     return (tx_active, rx_active)
  770.  
  771. TYPE6_STATUS_CODE_MAP = {
  772.     0: STATUS_PRINTER_IDLE,
  773.     -19928: STATUS_PRINTER_IDLE,
  774.     -18995: STATUS_PRINTER_CANCELING,
  775.     -17974: STATUS_PRINTER_WARMING_UP,
  776.     -17973: STATUS_PRINTER_PEN_CLEANING,
  777.     -18993: STATUS_PRINTER_BUSY,
  778.     -17949: STATUS_PRINTER_BUSY,
  779.     -19720: STATUS_PRINTER_MANUAL_DUPLEX_BLOCK,
  780.     -19678: STATUS_PRINTER_BUSY,
  781.     -19695: STATUS_PRINTER_OUT_OF_PAPER,
  782.     -17985: STATUS_PRINTER_MEDIA_JAM,
  783.     -19731: STATUS_PRINTER_OUT_OF_PAPER,
  784.     -18974: STATUS_PRINTER_BUSY,
  785.     -19730: STATUS_PRINTER_OUT_OF_PAPER,
  786.     -19729: STATUS_PRINTER_OUT_OF_PAPER,
  787.     -19933: STATUS_PRINTER_HARD_ERROR,
  788.     -17984: STATUS_PRINTER_DOOR_OPEN,
  789.     -19694: STATUS_PRINTER_DOOR_OPEN,
  790.     -18992: STATUS_PRINTER_MANUAL_FEED_BLOCKED,
  791.     -19690: STATUS_PRINTER_MEDIA_JAM,
  792.     -19689: STATUS_PRINTER_MEDIA_JAM,
  793.     -19611: STATUS_PRINTER_MEDIA_JAM,
  794.     -19686: STATUS_PRINTER_MEDIA_JAM,
  795.     -19688: STATUS_PRINTER_MEDIA_JAM,
  796.     -19685: STATUS_PRINTER_MEDIA_JAM,
  797.     -19684: STATUS_PRINTER_MEDIA_JAM,
  798.     -18848: STATUS_PRINTER_MEDIA_JAM,
  799.     -18847: STATUS_PRINTER_MEDIA_JAM,
  800.     -18846: STATUS_PRINTER_MEDIA_JAM,
  801.     -19687: STATUS_PRINTER_MEDIA_JAM,
  802.     -17992: STATUS_PRINTER_MEDIA_JAM,
  803.     -19700: STATUS_PRINTER_HARD_ERROR,
  804.     -17996: STATUS_PRINTER_FUSER_ERROR,
  805.     -17983: STATUS_PRINTER_FUSER_ERROR,
  806.     -17982: STATUS_PRINTER_FUSER_ERROR,
  807.     -17981: STATUS_PRINTER_FUSER_ERROR,
  808.     -17971: STATUS_PRINTER_FUSER_ERROR,
  809.     -17995: STATUS_PRINTER_HARD_ERROR,
  810.     -17994: STATUS_PRINTER_HARD_ERROR,
  811.     -17993: STATUS_PRINTER_HARD_ERROR,
  812.     -18994: STATUS_PRINTER_HARD_ERROR,
  813.     -17986: STATUS_PRINTER_HARD_ERROR,
  814.     -19904: STATUS_PRINTER_HARD_ERROR,
  815.     -19701: STATUS_PRINTER_NON_HP_INK,
  816.     -19613: STATUS_PRINTER_IDLE,
  817.     -19654: STATUS_PRINTER_NON_HP_INK,
  818.     -19682: STATUS_PRINTER_HARD_ERROR,
  819.     -19693: STATUS_PRINTER_IDLE,
  820.     -19752: STATUS_PRINTER_LOW_TONER,
  821.     -19723: STATUS_PRINTER_BUSY,
  822.     -19703: STATUS_PRINTER_BUSY,
  823.     -19739: STATUS_PRINTER_NO_TONER,
  824.     -19927: STATUS_PRINTER_BUSY,
  825.     -19932: STATUS_PRINTER_BUSY,
  826.     -19931: STATUS_PRINTER_BUSY,
  827.     -11989: STATUS_PRINTER_BUSY,
  828.     -11995: STATUS_PRINTER_BUSY,
  829.     -19954: STATUS_PRINTER_CANCELING,
  830.     -19955: STATUS_PRINTER_REPORT_PRINTING,
  831.     -19956: STATUS_PRINTER_REPORT_PRINTING,
  832.     -19934: STATUS_PRINTER_HARD_ERROR,
  833.     -19930: STATUS_PRINTER_BUSY,
  834.     -11990: STATUS_PRINTER_DOOR_OPEN,
  835.     -11999: STATUS_PRINTER_MEDIA_JAM,
  836.     -12000: STATUS_PRINTER_MEDIA_JAM,
  837.     -11998: STATUS_PRINTER_MEDIA_JAM,
  838.     -11986: STATUS_PRINTER_HARD_ERROR,
  839.     -11994: STATUS_PRINTER_BUSY,
  840.     -14967: STATUS_PRINTER_BUSY,
  841.     -19912: STATUS_PRINTER_HARD_ERROR,
  842.     -14962: STATUS_PRINTER_BUSY,
  843.     -14971: STATUS_PRINTER_BUSY,
  844.     -14973: STATUS_PRINTER_BUSY,
  845.     -14972: STATUS_PRINTER_BUSY,
  846.     -14966: STATUS_PRINTER_DOOR_OPEN,
  847.     -14974: STATUS_PRINTER_MEDIA_JAM,
  848.     -14969: STATUS_PRINTER_HARD_ERROR,
  849.     -14968: STATUS_PRINTER_HARD_ERROR,
  850.     -12996: STATUS_PRINTER_BUSY,
  851.     -12994: STATUS_PRINTER_BUSY,
  852.     -12993: STATUS_PRINTER_BUSY,
  853.     -12991: STATUS_PRINTER_BUSY,
  854.     -12995: STATUS_PRINTER_BUSY,
  855.     -12997: STATUS_PRINTER_HARD_ERROR,
  856.     -12990: STATUS_PRINTER_BUSY,
  857.     -12998: STATUS_PRINTER_BUSY,
  858.     -13000: STATUS_PRINTER_DOOR_OPEN,
  859.     -12999: STATUS_PRINTER_MEDIA_JAM,
  860.     -13859: STATUS_PRINTER_BUSY,
  861.     -13858: STATUS_PRINTER_BUSY,
  862.     -13868: STATUS_PRINTER_BUSY,
  863.     -13867: STATUS_PRINTER_BUSY,
  864.     -13857: STATUS_PRINTER_BUSY,
  865.     -13856: STATUS_PRINTER_BUSY,
  866.     -13855: STATUS_PRINTER_BUSY,
  867.     -13854: STATUS_PRINTER_BUSY,
  868.     -13853: STATUS_PRINTER_BUSY,
  869.     -13839: STATUS_PRINTER_BUSY,
  870.     -13842: STATUS_PRINTER_BUSY,
  871.     -13838: STATUS_PRINTER_BUSY,
  872.     -13847: STATUS_PRINTER_BUSY,
  873.     -13846: STATUS_PRINTER_BUSY,
  874.     -13845: STATUS_PRINTER_BUSY,
  875.     -13844: STATUS_PRINTER_BUSY,
  876.     -13840: STATUS_PRINTER_BUSY,
  877.     -13843: STATUS_PRINTER_BUSY,
  878.     -13850: STATUS_PRINTER_BUSY,
  879.     -13851: STATUS_PRINTER_BUSY,
  880.     -13836: STATUS_PRINTER_BUSY,
  881.     -13864: STATUS_PRINTER_BUSY,
  882.     -13863: STATUS_PRINTER_BUSY,
  883.     -13865: STATUS_PRINTER_BUSY,
  884.     -13862: STATUS_PRINTER_BUSY,
  885.     -13837: STATUS_PRINTER_BUSY,
  886.     -13861: STATUS_PRINTER_BUSY,
  887.     -13866: STATUS_PRINTER_BUSY,
  888.     -13841: STATUS_PRINTER_BUSY,
  889.     -13848: STATUS_PRINTER_BUSY,
  890.     -13849: STATUS_PRINTER_BUSY }
  891.  
  892. def StatusType6(dev):
  893.     info_device_status = cStringIO.StringIO()
  894.     info_ssp = cStringIO.StringIO()
  895.     
  896.     try:
  897.         dev.getEWSUrl('/hp/device/info_device_status.xml', info_device_status)
  898.         dev.getEWSUrl('/hp/device/info_ssp.xml', info_ssp)
  899.     except:
  900.         pass
  901.  
  902.     info_device_status = info_device_status.getvalue()
  903.     info_ssp = info_ssp.getvalue()
  904.     device_status = { }
  905.     ssp = { }
  906.     if info_device_status:
  907.         
  908.         try:
  909.             log.debug_block('info_device_status', info_device_status)
  910.             device_status = utils.XMLToDictParser().parseXML(info_device_status)
  911.             log.debug(device_status)
  912.         except expat.ExpatError:
  913.             log.error('Device Status XML parse error')
  914.             device_status = { }
  915.         except:
  916.             None<EXCEPTION MATCH>expat.ExpatError
  917.         
  918.  
  919.     None<EXCEPTION MATCH>expat.ExpatError
  920.     if info_ssp:
  921.         
  922.         try:
  923.             log.debug_block('info_spp', info_ssp)
  924.             ssp = utils.XMLToDictParser().parseXML(info_ssp)
  925.             log.debug(ssp)
  926.         except expat.ExpatError:
  927.             log.error('SSP XML parse error')
  928.             ssp = { }
  929.         except:
  930.             None<EXCEPTION MATCH>expat.ExpatError
  931.         
  932.  
  933.     None<EXCEPTION MATCH>expat.ExpatError
  934.     status_code = device_status.get('devicestatuspage-devicestatus-statuslist-status-code-0', 0)
  935.     if not status_code:
  936.         status_code = ssp.get('devicestatuspage-devicestatus-statuslist-status-code-0', 0)
  937.     
  938.     black_supply_level = device_status.get('devicestatuspage-suppliesstatus-blacksupply-percentremaining', 0)
  939.     black_supply_low = ssp.get('suppliesstatuspage-blacksupply-lowreached', 0)
  940.     agents = []
  941.     agents.append({
  942.         'kind': AGENT_KIND_TONER_CARTRIDGE,
  943.         'type': AGENT_TYPE_BLACK,
  944.         'health': 0,
  945.         'level': black_supply_level,
  946.         'level-trigger': 0 })
  947.     if dev.tech_type == TECH_TYPE_COLOR_LASER:
  948.         cyan_supply_level = device_status.get('devicestatuspage-suppliesstatus-cyansupply-percentremaining', 0)
  949.         agents.append({
  950.             'kind': AGENT_KIND_TONER_CARTRIDGE,
  951.             'type': AGENT_TYPE_CYAN,
  952.             'health': 0,
  953.             'level': cyan_supply_level,
  954.             'level-trigger': 0 })
  955.         magenta_supply_level = device_status.get('devicestatuspage-suppliesstatus-magentasupply-percentremaining', 0)
  956.         agents.append({
  957.             'kind': AGENT_KIND_TONER_CARTRIDGE,
  958.             'type': AGENT_TYPE_MAGENTA,
  959.             'health': 0,
  960.             'level': magenta_supply_level,
  961.             'level-trigger': 0 })
  962.         yellow_supply_level = device_status.get('devicestatuspage-suppliesstatus-yellowsupply-percentremaining', 0)
  963.         agents.append({
  964.             'kind': AGENT_KIND_TONER_CARTRIDGE,
  965.             'type': AGENT_TYPE_YELLOW,
  966.             'health': 0,
  967.             'level': yellow_supply_level,
  968.             'level-trigger': 0 })
  969.     
  970.     return {
  971.         'revision': STATUS_REV_UNKNOWN,
  972.         'agents': agents,
  973.         'top-door': 0,
  974.         'supply-door': 0,
  975.         'duplexer': 1,
  976.         'photo-tray': 0,
  977.         'in-tray1': 1,
  978.         'in-tray2': 1,
  979.         'media-path': 1,
  980.         'status-code': TYPE6_STATUS_CODE_MAP.get(status_code, STATUS_PRINTER_IDLE) }
  981.  
  982. PJL_STATUS_MAP = {
  983.     10001: STATUS_PRINTER_IDLE,
  984.     10002: STATUS_PRINTER_OFFLINE,
  985.     10003: STATUS_PRINTER_WARMING_UP,
  986.     10004: STATUS_PRINTER_BUSY,
  987.     10005: STATUS_PRINTER_BUSY,
  988.     10006: STATUS_PRINTER_LOW_TONER,
  989.     10007: STATUS_PRINTER_CANCELING,
  990.     10010: STATUS_PRINTER_SERVICE_REQUEST,
  991.     10011: STATUS_PRINTER_OFFLINE,
  992.     10013: STATUS_PRINTER_BUSY,
  993.     10014: STATUS_PRINTER_REPORT_PRINTING,
  994.     10015: STATUS_PRINTER_BUSY,
  995.     10016: STATUS_PRINTER_BUSY,
  996.     10017: STATUS_PRINTER_REPORT_PRINTING,
  997.     10018: STATUS_PRINTER_BUSY,
  998.     10019: STATUS_PRINTER_BUSY,
  999.     10020: STATUS_PRINTER_BUSY,
  1000.     10021: STATUS_PRINTER_BUSY,
  1001.     10022: STATUS_PRINTER_REPORT_PRINTING,
  1002.     10023: STATUS_PRINTER_PRINTING,
  1003.     10024: STATUS_PRINTER_SERVICE_REQUEST,
  1004.     10025: STATUS_PRINTER_SERVICE_REQUEST,
  1005.     10026: STATUS_PRINTER_BUSY,
  1006.     10027: STATUS_PRINTER_MEDIA_JAM,
  1007.     10028: STATUS_PRINTER_REPORT_PRINTING,
  1008.     10029: STATUS_PRINTER_PRINTING,
  1009.     10030: STATUS_PRINTER_BUSY,
  1010.     10031: STATUS_PRINTER_BUSY,
  1011.     10032: STATUS_PRINTER_BUSY,
  1012.     10033: STATUS_PRINTER_SERVICE_REQUEST,
  1013.     10034: STATUS_PRINTER_CANCELING,
  1014.     10035: STATUS_PRINTER_PRINTING,
  1015.     10036: STATUS_PRINTER_WARMING_UP,
  1016.     10200: STATUS_PRINTER_LOW_BLACK_TONER,
  1017.     10201: STATUS_PRINTER_LOW_CYAN_TONER,
  1018.     10202: STATUS_PRINTER_LOW_MAGENTA_TONER,
  1019.     10203: STATUS_PRINTER_LOW_YELLOW_TONER,
  1020.     10204: STATUS_PRINTER_LOW_TONER,
  1021.     10205: STATUS_PRINTER_LOW_BLACK_TONER,
  1022.     10206: STATUS_PRINTER_LOW_CYAN_TONER,
  1023.     10207: STATUS_PRINTER_LOW_MAGENTA_TONER,
  1024.     10208: STATUS_PRINTER_LOW_YELLOW_TONER,
  1025.     10209: STATUS_PRINTER_LOW_BLACK_TONER,
  1026.     10210: STATUS_PRINTER_LOW_CYAN_TONER,
  1027.     10211: STATUS_PRINTER_LOW_MAGENTA_TONER,
  1028.     10212: STATUS_PRINTER_LOW_YELLOW_TONER,
  1029.     10213: STATUS_PRINTER_SERVICE_REQUEST,
  1030.     10214: STATUS_PRINTER_SERVICE_REQUEST,
  1031.     10215: STATUS_PRINTER_SERVICE_REQUEST,
  1032.     10216: STATUS_PRINTER_SERVICE_REQUEST,
  1033.     10217: STATUS_PRINTER_SERVICE_REQUEST,
  1034.     10218: STATUS_PRINTER_LOW_TONER,
  1035.     10300: STATUS_PRINTER_LOW_BLACK_TONER,
  1036.     10301: STATUS_PRINTER_LOW_CYAN_TONER,
  1037.     10302: STATUS_PRINTER_LOW_MAGENTA_TONER,
  1038.     10303: STATUS_PRINTER_LOW_YELLOW_TONER,
  1039.     10304: STATUS_PRINTER_SERVICE_REQUEST,
  1040.     10305: STATUS_PRINTER_SERVICE_REQUEST,
  1041.     10306: STATUS_PRINTER_SERVICE_REQUEST,
  1042.     10307: STATUS_PRINTER_SERVICE_REQUEST,
  1043.     10308: STATUS_PRINTER_SERVICE_REQUEST,
  1044.     10309: STATUS_PRINTER_SERVICE_REQUEST,
  1045.     10310: STATUS_PRINTER_SERVICE_REQUEST,
  1046.     10311: STATUS_PRINTER_SERVICE_REQUEST,
  1047.     10312: STATUS_PRINTER_SERVICE_REQUEST,
  1048.     10313: STATUS_PRINTER_SERVICE_REQUEST,
  1049.     10314: STATUS_PRINTER_SERVICE_REQUEST,
  1050.     10315: STATUS_PRINTER_SERVICE_REQUEST,
  1051.     10316: STATUS_PRINTER_SERVICE_REQUEST,
  1052.     10317: STATUS_PRINTER_SERVICE_REQUEST,
  1053.     10318: STATUS_PRINTER_SERVICE_REQUEST,
  1054.     10400: STATUS_PRINTER_NON_HP_INK,
  1055.     10401: STATUS_PRINTER_IDLE,
  1056.     10402: STATUS_PRINTER_SERVICE_REQUEST,
  1057.     10403: STATUS_PRINTER_IDLE,
  1058.     30119: STATUS_PRINTER_MEDIA_JAM,
  1059.     40021: STATUS_PRINTER_DOOR_OPEN,
  1060.     40022: STATUS_PRINTER_MEDIA_JAM,
  1061.     40038: STATUS_PRINTER_LOW_TONER,
  1062.     40600: STATUS_PRINTER_NO_TONER }
  1063. MIN_PJL_ERROR_CODE = 10001
  1064. DEFAULT_PJL_ERROR_CODE = 10001
  1065.  
  1066. def MapPJLErrorCode(error_code, str_code = None):
  1067.     if error_code < MIN_PJL_ERROR_CODE:
  1068.         return STATUS_PRINTER_BUSY
  1069.     if str_code is None:
  1070.         str_code = str(error_code)
  1071.     
  1072.     if len(str_code) < 5:
  1073.         return STATUS_PRINTER_BUSY
  1074.     status_code = PJL_STATUS_MAP.get(error_code, None)
  1075.     log.debug('Mapped PJL error code %d to status code %d' % (error_code, status_code))
  1076.     return status_code
  1077.  
  1078. pjl_code_pat = re.compile('^CODE\\s*=\\s*(\\d.*)$', re.IGNORECASE)
  1079.  
  1080. def StatusType8(dev):
  1081.     
  1082.     try:
  1083.         dev.openPrint()
  1084.     except Error:
  1085.         e = None
  1086.         log.warn(e.msg)
  1087.         status_code = STATUS_PRINTER_BUSY
  1088.  
  1089.     
  1090.     try:
  1091.         dev.writePrint('\x1b%-12345X@PJL INFO STATUS \r\n\x1b%-12345X')
  1092.         pjl_return = dev.readPrint(1024, timeout = 5, allow_short_read = True)
  1093.         dev.close()
  1094.         log.debug_block('PJL return:', pjl_return)
  1095.         str_code = '10001'
  1096.         for line in pjl_return.splitlines():
  1097.             line = line.strip()
  1098.             match = pjl_code_pat.match(line)
  1099.             if match is not None:
  1100.                 str_code = match.group(1)
  1101.                 break
  1102.                 continue
  1103.         
  1104.         log.debug('Code = %s' % str_code)
  1105.         
  1106.         try:
  1107.             error_code = int(str_code)
  1108.         except ValueError:
  1109.             error_code = DEFAULT_PJL_ERROR_CODE
  1110.  
  1111.         log.debug('Error code = %d' % error_code)
  1112.         status_code = MapPJLErrorCode(error_code, str_code)
  1113.     except Error:
  1114.         status_code = STATUS_PRINTER_HARD_ERROR
  1115.     finally:
  1116.         
  1117.         try:
  1118.             dev.closePrint()
  1119.         except Error:
  1120.             pass
  1121.  
  1122.  
  1123.     agents = []
  1124.     if status_code in (STATUS_PRINTER_LOW_TONER, STATUS_PRINTER_LOW_BLACK_TONER):
  1125.         health = AGENT_HEALTH_OK
  1126.         level_trigger = AGENT_LEVEL_TRIGGER_MAY_BE_LOW
  1127.         level = 0
  1128.     elif status_code == STATUS_PRINTER_NO_TONER:
  1129.         health = AGENT_HEALTH_MISINSTALLED
  1130.         level_trigger = AGENT_LEVEL_TRIGGER_MAY_BE_LOW
  1131.         level = 0
  1132.     else:
  1133.         health = AGENT_HEALTH_OK
  1134.         level_trigger = AGENT_LEVEL_TRIGGER_SUFFICIENT_0
  1135.         level = 100
  1136.     log.debug('Agent: health=%d, level=%d, trigger=%d' % (health, level, level_trigger))
  1137.     agents.append({
  1138.         'kind': AGENT_KIND_TONER_CARTRIDGE,
  1139.         'type': AGENT_TYPE_BLACK,
  1140.         'health': health,
  1141.         'level': level,
  1142.         'level-trigger': level_trigger })
  1143.     if dev.tech_type == TECH_TYPE_COLOR_LASER:
  1144.         level = 100
  1145.         level_trigger = AGENT_LEVEL_TRIGGER_SUFFICIENT_0
  1146.         if status_code == STATUS_PRINTER_LOW_CYAN_TONER:
  1147.             level = 0
  1148.             level_trigger = AGENT_LEVEL_TRIGGER_MAY_BE_LOW
  1149.         
  1150.         log.debug('Agent: health=%d, level=%d, trigger=%d' % (health, level, level_trigger))
  1151.         agents.append({
  1152.             'kind': AGENT_KIND_TONER_CARTRIDGE,
  1153.             'type': AGENT_TYPE_CYAN,
  1154.             'health': AGENT_HEALTH_OK,
  1155.             'level': level,
  1156.             'level-trigger': level_trigger })
  1157.         level = 100
  1158.         level_trigger = AGENT_LEVEL_TRIGGER_SUFFICIENT_0
  1159.         if status_code == STATUS_PRINTER_LOW_MAGENTA_TONER:
  1160.             level = 0
  1161.             level_trigger = AGENT_LEVEL_TRIGGER_MAY_BE_LOW
  1162.         
  1163.         log.debug('Agent: health=%d, level=%d, trigger=%d' % (health, level, level_trigger))
  1164.         agents.append({
  1165.             'kind': AGENT_KIND_TONER_CARTRIDGE,
  1166.             'type': AGENT_TYPE_MAGENTA,
  1167.             'health': AGENT_HEALTH_OK,
  1168.             'level': level,
  1169.             'level-trigger': level_trigger })
  1170.         level = 100
  1171.         level_trigger = AGENT_LEVEL_TRIGGER_SUFFICIENT_0
  1172.         if status_code == STATUS_PRINTER_LOW_YELLOW_TONER:
  1173.             level = 0
  1174.             level_trigger = AGENT_LEVEL_TRIGGER_MAY_BE_LOW
  1175.         
  1176.         log.debug('Agent: health=%d, level=%d, trigger=%d' % (health, level, level_trigger))
  1177.         agents.append({
  1178.             'kind': AGENT_KIND_TONER_CARTRIDGE,
  1179.             'type': AGENT_TYPE_YELLOW,
  1180.             'health': AGENT_HEALTH_OK,
  1181.             'level': level,
  1182.             'level-trigger': level_trigger })
  1183.     
  1184.     if status_code == 40021:
  1185.         top_door = 0
  1186.     else:
  1187.         top_door = 1
  1188.     log.debug('Status code = %d' % status_code)
  1189.     return {
  1190.         'revision': STATUS_REV_UNKNOWN,
  1191.         'agents': agents,
  1192.         'top-door': top_door,
  1193.         'supply-door': top_door,
  1194.         'duplexer': 0,
  1195.         'photo-tray': 0,
  1196.         'in-tray1': 1,
  1197.         'in-tray2': 1,
  1198.         'media-path': 1,
  1199.         'status-code': status_code }
  1200.  
  1201.